﻿using System;
using System.Linq;
using System.Linq.Expressions;

namespace NotMyself.Oxite.Data.LinqToNHibernateProvider.Specification
{
    public abstract class SpecificationBase<T>
    {
        public abstract Expression<Func<T, bool>> MatchingCriteria { get; }

        public T SatisfyingElementFrom(IQueryable<T> candidates)
        {
            return SatisfyingElementsFrom(candidates).Single();
        }

        public IQueryable<T> SatisfyingElementsFrom(IQueryable<T> candidates)
        {
            return candidates.Where(MatchingCriteria).AsQueryable();
        }

        public bool IsSatisfiedBy(T candidate)
        {
            return SatisfyingElementsFrom(new[] {candidate}.AsQueryable()).Any();
        }
    }
}

//using System;
//using System.Linq.Expressions;
//using AllianceEnterprises.Domain.Entities;

//namespace AllianceEnterprises.Data.Specification
//{
//  public class VendorByOrderCountQuery : SpecificationBase<Vendor>
//  {
//    private readonly int _orderCount;

//    public VendorByOrderCountQuery(int orderCount)
//    {
//      _orderCount = orderCount;
//    }


//    public override Expression<Func<Vendor, bool>> MatchingCriteria
//    {
//      get { return cust => cust.Id == _orderCount; }
//    }
//  }
//}